<?php

namespace app\admin\controller\qingdongams\general;

use app\admin\controller\qingdongams\Base;
use app\common\library\Auth;
use think\Db;
use think\Exception;

/**
 * 表单文本模板
 */
class Texttemplate extends Base {
	protected $relationSearch = true;
	/**
	 * @var \addons\qingdongams\model\TextTemplate
	 */
	protected $model = null;

	public function _initialize() {
		parent::_initialize();
		$this->model = new \addons\qingdongams\model\TextTemplate;
	}


	/**
	 * 查看
	 */
	public function index() {
		//设置过滤方法
		$this->request->filter(['strip_tags', 'trim']);
		if ($this->request->isAjax()) {

			$types = $this->model->where([])->select();
			$types = collection($types)->toArray();
			foreach ($types as $k => $v) {
				$v['content'] = json_decode($v['content'], true);
				if (is_array($v['content'])) {
					$content = [];
					foreach ($v['content'] as $ves) {
						$content[] = $ves['field'];
					}
					$v['content'] = implode('，', $content);
				}
				$types[$k] = $v;
			}
			$result = array("total" => count($types), "rows" => $types);

			return json($result);
		}

		return $this->view->fetch();
	}


	/**
	 * 添加
	 */
	public function add() {
		if ($this->request->isPost()) {
			$params = $this->request->post("row/a");
			if ($params) {
				$params         = $this->preExcludeFields($params);
				$add            = [];
				$add['type'] = $params['type'];
               if($this->model->where(['type'=>$params['type']])->find()){
                   $this->error('模板类型已存在');
               }
				$add['content'] = ($params['content']);
				$result         = false;
				Db::startTrans();
				try {
					$result             = $this->model::create($add);
					Db::commit();
				} catch (Exception $e) {
					Db::rollback();
					$this->error($e->getMessage());
				}
				if ($result !== false) {
					$this->success();
				} else {
					$this->error(__('No rows were inserted'));
				}
			}
			$this->error(__('Parameter %s can not be empty', ''));
		}

		return $this->view->fetch();
	}


	/**
	 * 修改
	 */
	public function edit($ids = null) {
		$row = $this->model->get($ids);
		if ($this->request->isPost()) {
			$params = $this->request->post("row/a");
			if ($params) {
				$params            = $this->preExcludeFields($params);
				$edit            = [];
				$edit['type']    = $params['type'];
				$edit['content'] = ($params['content']);

				$result            = false;
				Db::startTrans();
				try {
					$edit['id'] = $ids;
					$result       = $this->model->isUpdate(true)->save($edit);
					Db::commit();
				} catch (Exception $e) {
					Db::rollback();
					$this->error($e->getMessage());
				}
				if ($result !== false) {
					$this->success();
				} else {
					$this->error(__('No rows were inserted'));
				}
			}
			$this->error(__('Parameter %s can not be empty', ''));
		}

		$this->assign('row', $row);

		return $this->view->fetch();
	}


	/**
	 * 删除
	 */
	public function del($ids = "") {
		if (!$this->request->isPost()) {
			$this->error(__("Invalid parameters"));
		}
		$ids                 = $ids ? $ids : $this->request->post("ids");
		$row                 = $this->model->get($ids);
		$this->modelValidate = true;
		if (!$row) {
			$this->error(__('No Results were found'));
		}
		Auth::instance()->delete($row['id']);
		$this->success();
	}
}
